/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View.seguridad;

import Model.Parametro;
import Presenter.EnvioPedidoPresenter;
import Presenter.ParametroPresenter;
import Principals.Validadores;
import View.components.Index;
import java.math.BigDecimal;
import javax.swing.JDesktopPane;
import javax.swing.JOptionPane;

/**
 *
 * @author walter
 */
public class Frm_ModificarVariableSistema extends javax.swing.JDesktopPane {

    Parametro p;
    Validadores val = new Validadores();

    /**
     * Creates new form Frm_BuscarVariableSistema
     */
    public Frm_ModificarVariableSistema(int idVariable) {
        EnvioPedidoPresenter.actualizarEnvioPedido() ;
        p = ParametroPresenter.cargarParametro(idVariable);
        initComponents();
        val.desactivarAccionPegar(txtNombre);
        val.desactivarAccionPegar(txtValorNumerico);
        val.desactivarAccionPegar(txtValorString);

        txtNombre.setText(p.getNombre());
        if ((!"".equals(p.getValorString())) && (p.getValorString() != null)) {
            txtValorString.setText(p.getValorString());
            txtValorNumerico.setEnabled(false);
        } else {
            txtValorNumerico.setText(p.getValorNumerico().toString());
            txtValorString.setEnabled(false);
        }
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jToolBar2 = new javax.swing.JToolBar();
        btnToolNuevo = new javax.swing.JButton();
        btnToolModificar = new javax.swing.JButton();
        btnToolEliminar = new javax.swing.JButton();
        btnToolAlmacenar = new javax.swing.JButton();
        jSeparator3 = new javax.swing.JToolBar.Separator();
        btnToolLimpar = new javax.swing.JButton();
        btnToolBuscar = new javax.swing.JButton();
        jSeparator4 = new javax.swing.JToolBar.Separator();
        btnToolCancelar = new javax.swing.JButton();
        pnlBuscarVariableSistema = new javax.swing.JPanel();
        lblNombre = new javax.swing.JLabel();
        lblValor = new javax.swing.JLabel();
        txtNombre = new javax.swing.JTextField();
        lblMsgNombre1 = new javax.swing.JLabel();
        lblMsgNombre2 = new javax.swing.JLabel();
        lblMsgNombre3 = new javax.swing.JLabel();
        lblValor1 = new javax.swing.JLabel();
        txtValorString = new javax.swing.JTextField();
        txtValorNumerico = new javax.swing.JTextField();

        jToolBar2.setFloatable(false);
        jToolBar2.setBorderPainted(false);

        btnToolNuevo.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Nuevo.png"))); // NOI18N
        btnToolNuevo.setEnabled(false);
        btnToolNuevo.setFocusable(false);
        btnToolNuevo.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnToolNuevo.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnToolNuevo.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToolNuevoActionPerformed(evt);
            }
        });
        jToolBar2.add(btnToolNuevo);

        btnToolModificar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Editar.png"))); // NOI18N
        btnToolModificar.setEnabled(false);
        btnToolModificar.setFocusable(false);
        btnToolModificar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnToolModificar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnToolModificar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToolModificarActionPerformed(evt);
            }
        });
        jToolBar2.add(btnToolModificar);

        btnToolEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Eliminar.png"))); // NOI18N
        btnToolEliminar.setEnabled(false);
        btnToolEliminar.setFocusable(false);
        btnToolEliminar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnToolEliminar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnToolEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToolEliminarActionPerformed(evt);
            }
        });
        jToolBar2.add(btnToolEliminar);

        btnToolAlmacenar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Guardar.png"))); // NOI18N
        btnToolAlmacenar.setFocusable(false);
        btnToolAlmacenar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnToolAlmacenar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnToolAlmacenar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToolAlmacenarActionPerformed(evt);
            }
        });
        jToolBar2.add(btnToolAlmacenar);
        jToolBar2.add(jSeparator3);

        btnToolLimpar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Limpiar.png"))); // NOI18N
        btnToolLimpar.setEnabled(false);
        btnToolLimpar.setFocusable(false);
        btnToolLimpar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnToolLimpar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        jToolBar2.add(btnToolLimpar);

        btnToolBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Buscar.png"))); // NOI18N
        btnToolBuscar.setEnabled(false);
        btnToolBuscar.setFocusable(false);
        btnToolBuscar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnToolBuscar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnToolBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToolBuscarActionPerformed(evt);
            }
        });
        jToolBar2.add(btnToolBuscar);
        jToolBar2.add(jSeparator4);

        btnToolCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Images/botones/Cancelar.png"))); // NOI18N
        btnToolCancelar.setToolTipText("");
        btnToolCancelar.setFocusable(false);
        btnToolCancelar.setHorizontalTextPosition(javax.swing.SwingConstants.CENTER);
        btnToolCancelar.setVerticalTextPosition(javax.swing.SwingConstants.BOTTOM);
        btnToolCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnToolCancelarActionPerformed(evt);
            }
        });
        jToolBar2.add(btnToolCancelar);

        pnlBuscarVariableSistema.setBorder(javax.swing.BorderFactory.createTitledBorder("Modificar"));
        pnlBuscarVariableSistema.setCursor(new java.awt.Cursor(java.awt.Cursor.DEFAULT_CURSOR));

        lblNombre.setText("Nombre*:");

        lblValor.setText("Valor string:");

        txtNombre.setEnabled(false);
        txtNombre.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtNombreKeyTyped(evt);
            }
        });

        lblMsgNombre1.setText("(*) Todo nombre de una variable del sistema debe contener las unidades en que se representa su valor. ");

        lblMsgNombre2.setText("Por ejemplo:");

        lblMsgNombre3.setText("Nombre: Costo de galón de gasolina (S/.)");

        lblValor1.setText("Valor numérico:");

        txtValorString.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtValorStringKeyPressed(evt);
            }
        });

        txtValorNumerico.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtValorNumericoKeyTyped(evt);
            }
        });

        javax.swing.GroupLayout pnlBuscarVariableSistemaLayout = new javax.swing.GroupLayout(pnlBuscarVariableSistema);
        pnlBuscarVariableSistema.setLayout(pnlBuscarVariableSistemaLayout);
        pnlBuscarVariableSistemaLayout.setHorizontalGroup(
            pnlBuscarVariableSistemaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlBuscarVariableSistemaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlBuscarVariableSistemaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnlBuscarVariableSistemaLayout.createSequentialGroup()
                        .addComponent(lblMsgNombre2)
                        .addGap(18, 18, 18)
                        .addComponent(lblMsgNombre3))
                    .addGroup(pnlBuscarVariableSistemaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(pnlBuscarVariableSistemaLayout.createSequentialGroup()
                            .addGroup(pnlBuscarVariableSistemaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(lblValor)
                                .addComponent(lblNombre))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                            .addGroup(pnlBuscarVariableSistemaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addGroup(pnlBuscarVariableSistemaLayout.createSequentialGroup()
                                    .addComponent(txtValorString, javax.swing.GroupLayout.PREFERRED_SIZE, 132, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addGap(58, 58, 58)
                                    .addComponent(lblValor1)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                    .addComponent(txtValorNumerico))
                                .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 419, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGap(0, 0, Short.MAX_VALUE))
                        .addComponent(lblMsgNombre1, javax.swing.GroupLayout.PREFERRED_SIZE, 528, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        pnlBuscarVariableSistemaLayout.setVerticalGroup(
            pnlBuscarVariableSistemaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnlBuscarVariableSistemaLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnlBuscarVariableSistemaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblNombre)
                    .addComponent(txtNombre, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(pnlBuscarVariableSistemaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblValor)
                    .addComponent(lblValor1)
                    .addComponent(txtValorString, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtValorNumerico, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 32, Short.MAX_VALUE)
                .addComponent(lblMsgNombre1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnlBuscarVariableSistemaLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(lblMsgNombre2)
                    .addComponent(lblMsgNombre3))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jToolBar2, javax.swing.GroupLayout.DEFAULT_SIZE, 582, Short.MAX_VALUE)
                    .addComponent(pnlBuscarVariableSistema, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jToolBar2, javax.swing.GroupLayout.PREFERRED_SIZE, 25, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(pnlBuscarVariableSistema, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addContainerGap())
        );
    }// </editor-fold>//GEN-END:initComponents

    private void cambiarVista(JDesktopPane panel) {
        ((Index) (this.getTopLevelAncestor())).changeDesktopPane(panel);
    }

    private void btnToolNuevoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToolNuevoActionPerformed
    }//GEN-LAST:event_btnToolNuevoActionPerformed

    private void btnToolModificarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToolModificarActionPerformed
    }//GEN-LAST:event_btnToolModificarActionPerformed

    private void btnToolBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToolBuscarActionPerformed
    }//GEN-LAST:event_btnToolBuscarActionPerformed

    private void btnToolEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToolEliminarActionPerformed
    }//GEN-LAST:event_btnToolEliminarActionPerformed

    private void btnToolCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToolCancelarActionPerformed
        cambiarVista(new Frm_BuscarVariableSistema());
    }//GEN-LAST:event_btnToolCancelarActionPerformed

    private void btnToolAlmacenarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnToolAlmacenarActionPerformed
        int respuesta = JOptionPane.showConfirmDialog(this, "Desea guardar los cambios", "Modificar", JOptionPane.OK_CANCEL_OPTION);
        switch (respuesta) {
            case JOptionPane.OK_OPTION:
                p.setNombre(txtNombre.getText());
                if (txtValorString.isEnabled()) {
                    p.setValorString(txtValorString.getText());
                } else {
                    p.setValorNumerico(new BigDecimal(txtValorNumerico.getText()));
                }
                ParametroPresenter.updateParametro(p);
                JOptionPane.showMessageDialog(this, "Se actualizó la información del parámetro");
                cambiarVista(new Frm_BuscarVariableSistema());
                break;
            case JOptionPane.CANCEL_OPTION:
                break;
        }
    }//GEN-LAST:event_btnToolAlmacenarActionPerformed

    private void txtNombreKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtNombreKeyTyped
        val.validaCaracteresRaros(evt);
    }//GEN-LAST:event_txtNombreKeyTyped

    private void txtValorStringKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtValorStringKeyPressed
        val.validaSoloText(evt);
    }//GEN-LAST:event_txtValorStringKeyPressed

    private void txtValorNumericoKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtValorNumericoKeyTyped
        val.validaDouble(evt);
    }//GEN-LAST:event_txtValorNumericoKeyTyped

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnToolAlmacenar;
    private javax.swing.JButton btnToolBuscar;
    private javax.swing.JButton btnToolCancelar;
    private javax.swing.JButton btnToolEliminar;
    private javax.swing.JButton btnToolLimpar;
    private javax.swing.JButton btnToolModificar;
    private javax.swing.JButton btnToolNuevo;
    private javax.swing.JToolBar.Separator jSeparator3;
    private javax.swing.JToolBar.Separator jSeparator4;
    private javax.swing.JToolBar jToolBar2;
    private javax.swing.JLabel lblMsgNombre1;
    private javax.swing.JLabel lblMsgNombre2;
    private javax.swing.JLabel lblMsgNombre3;
    private javax.swing.JLabel lblNombre;
    private javax.swing.JLabel lblValor;
    private javax.swing.JLabel lblValor1;
    private javax.swing.JPanel pnlBuscarVariableSistema;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JTextField txtValorNumerico;
    private javax.swing.JTextField txtValorString;
    // End of variables declaration//GEN-END:variables
}
